#ifdef KINECT

#include "inc\GUI\kinect\kinectsnapshotrecorderframe.h"
#include "ui_kinectsnapshotrecorderframe.h"

KinectSnapshotRecorderFrame::KinectSnapshotRecorderFrame(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::KinectSnapshotRecorderFrame),
    _frameSize(175)
{
    ui->setupUi(this);
    _flowLayout = new FlowLayout(ui->snapshots);

    connect(ui->startButton, SIGNAL(clicked()), this, SLOT(requestStart()));
    connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(requestStop()));

    stopped();
}

KinectSnapshotRecorderFrame::~KinectSnapshotRecorderFrame() {
    delete ui;
}

void KinectSnapshotRecorderFrame::started() {
    ui->startButton->setDisabled(true);
    ui->stopButton->setDisabled(false);
}

void KinectSnapshotRecorderFrame::stopped() {
    ui->startButton->setDisabled(false);
    ui->stopButton->setDisabled(true);
}

void KinectSnapshotRecorderFrame::addPose(int id, MotionKeyFrame pose) {
    //frameSize = 80;
    int margin = 3;
    int columnCount = qFloor((ui->scrollArea->width()-margin)/(_frameSize+margin));

    TroyFrame* troy = new TroyFrame(ui->snapshots);
    troy->loadKeyFrame(pose);
    troy->setMinimumSize(_frameSize, _frameSize);
    troy->mirror(true);
    //troy->move(margin + (_frameSize + margin) * (_poses.size()%columnCount), margin + (_frameSize + margin) * qFloor(_poses.size()/columnCount));
    //troy->setFrameShadow(QFrame::Raised);
    //troy->setFrameShape(QFrame::Panel);
    QPalette palette = troy->palette();
    palette.setColor(QPalette::Background, QColor(0, 223, 91));
    troy->setPalette(palette);
    troy->setAutoFillBackground(false);
    //troy->setLineWidth(15);
    //troy->show();
    //ui->snapshots->setMinimumSize(margin + (_frameSize + margin) * columnCount, margin + (_frameSize + margin) * (1+qFloor(_poses.size()/columnCount)));

    _flowLayout->addWidget(troy);

    _poses.insert(id, troy);
}

void KinectSnapshotRecorderFrame::deletePose(int id) {
    if (_poses.contains(id)) {
        _poses.find(id).value()->deleteLater();
        _poses.remove(id);
    }
}

void KinectSnapshotRecorderFrame::startPose(int id) {
    qDebug() << "Start Pose:" << id;
    if (_poses.contains(id)) {
        TroyFrame* frame = _poses.find(id).value();
        frame->setAutoFillBackground(true);
    }
}

void KinectSnapshotRecorderFrame::endPose(int id) {
    qDebug() << "End Pose:" << id;
    if (_poses.contains(id)) {
        TroyFrame* frame = _poses.find(id).value();
        frame->setAutoFillBackground(false);
    }
}

void KinectSnapshotRecorderFrame::setPoseMatch(int id, float closeness) {
    //qDebug() << "Pose Match:" << id << closeness;
    if (_poses.contains(id)) {
        TroyFrame* troy = _poses.find(id).value();
        //int oldWidth = troy->width();
        //int oldHeight = troy->height();

        //int newSize = _frameSize * closeness;

        //int deltaX = qRound((oldWidth - _frameSize)/(float)2.0 + (_frameSize * (1 - closeness))/(float)2.0);
        //int deltaY = qRound((oldHeight - _frameSize)/(float)2.0 + (_frameSize * (1 - closeness))/(float)2.0);

        //troy->move(troy->x() + deltaX, troy->y() + deltaY);
        //troy->resize(newSize, newSize);
        //troy->setLineWidth(0.25*_frameSize*closeness);

        troy->setTroyScale(0.5 + (closeness * 0.4));
    }
}

void KinectSnapshotRecorderFrame::requestStart() {
    emit startRequested();
}

void KinectSnapshotRecorderFrame::requestStop() {
    emit stopRequested();
}

#endif
